;--------------------------------------------------------------------
;SOUND BANK AND ROUNTINES
;--------------------------------------------------------------------

SMP01	equ		0
SMP02	equ		(SMP01+3)
SMP03	equ		(SMP02+3)
SMP04	equ		(SMP03+3)
SMP05	equ		(SMP04+3)
SMP06	equ		(SMP05+3)

PRS		equ		SMP01
PFS		equ		SMP03
LRS		equ		SMP02
LFS		equ		SMP04
TSS		equ		SMP05

callsample	macro sample
	movlw	sample
	call	load_sample
	call	generate_sound
	endm

sample_bank:
	addwf	PCL
	;sample01	PFS
	retlw	D'45'			;freq
	retlw	D'10'			;t signal
	retlw	D'5'			;t pause
	;sample02	PRS
	retlw	D'39'			;freq
	retlw	D'10'			;t signal
	retlw	D'5'			;t pause

;----------------------------------
;LOADING SAMPLE
;----------------------------------
load_sample:
	movwf	cnt1
	movlw	timp					;set pointer to first register address
	movwf	FSR						;and set FSR register
	movlw	NO_PARAMS				;number of parameters
	movwf	cnt2					;load variable as counter
	movf	cnt1, W					;set to begining of given sample
load_next_parameter:
	call	sample_bank				;loading a piece of sample from bank
	movwf	INDF					;copying value of from bank to register
	incf	cnt1, F					;increment pointer to the element of given sample...
	movf	cnt1, W					;and copy given value to W (W is as function argument)
	incf	FSR,F					;increment the pointer of reg file
	decfsz	cnt2
	goto	load_next_parameter		;if not zero get next parameter
	retlw	0						;return
;----------------------------------
;GENERATING SOUND
;Sound output:
;GP0
;----------------------------------
generate_sound:
	movlw	b'10000001'				;setting of timer0 prescaler
	option
	movf	ts,W
	movwf	cnt2
	movlw	D'10'
	movwf	cnt3
signal_loop:
	bsf		GPIO,PIEZO_OUT_PIN
	movf	timp,W
	movwf	cnt1
high_impulse_delay:					;delay=4us+(timp*14us)
	call	wait10ust
	decfsz	cnt1
	goto	high_impulse_delay
	nop
	bcf		GPIO,PIEZO_OUT_PIN
	movf	timp,W
	movwf	cnt1
low_impulse_delay:
	call	wait10ust
	decfsz	cnt1
	goto	low_impulse_delay
	decfsz	cnt3
	goto	signal_loop
	movlw	D'10'					;hard coded delay parameter
	movwf	cnt3					;set low counter
	decfsz	cnt2					;decrement high counter, jump if zero
	goto	signal_loop				;if not zero
pause_loop_init:
	movf	tp,W					;copy tp parameter
	movwf	cnt1					;to cnt1 register
pause_loop:
	call	wait10mst				;call wait routine

	decfsz	cnt1
	goto	pause_loop

	retlw	0